// class BubbleSort{
  function sort(ary) {
    let temp = 0
    let lastExchangeIndex =0
    let sortBorder = ary.length-1
    for(let i =0; i<ary.length; i++) {
      let isSorted = true
      for(let j = 0; j < sortBorder; j++) {
        if(ary[j] > ary[j+1]) {
          temp = ary[j]
          ary[j] = ary[j+1]
          ary[j+1] = temp
          isSorted = false
          lastExchangeIndex = j
        }
      }
      sortBorder = lastExchangeIndex
      if(isSorted) {
        break
      }
    }
    return ary
  }
// }

let array = [3,4,2,1,5,6,7,8]
let sortAry = sort(array)
console.log(sortAry)